package com.jron.cqiu.kernel.implement.windows;

import com.jron.cqiu.kernel.mail.MailOPattern;
import com.jron.cqiu.kernel.cmdline.XP;
import com.jron.cqiu.kernel.exception.OrderExecFailedException;
import org.jvnet.winp.ExitWindows;
import org.jvnet.winp.ExitWindows.Flag;
import static org.jvnet.winp.ExitWindows.Flag.FORCEIFHUNG;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Created by Intellij IDEA.
 *
 * @Author: Yejw
 * @Date: 2008-6-20
 * @Time: 12:31:22
 */
public class WinExecution {

    private static Log log = LogFactory.getLog(WinExecution.class);

    public void execMailOrder(String orderMethod, String... args) throws OrderExecFailedException {
        MailOPattern mo = MailOPattern.getEnum(orderMethod);

        /*
        if (mo.compareTo(MailOPattern.HAKUNAMATATA) == 0) { }
        if (mo.compareTo(MailOPattern.KILL) == 0) { }
        if (mo.compareTo(MailOPattern.KILL_PID) == 0) { }
        */
        if (mo.compareTo(MailOPattern.SHUTDOWN) == 0) {
            if (args.length == 1) {
                try {
                    Flag f = Flag.valueOf(Flag.class, args[0].toUpperCase());
                    ExitWindows.powerOff(f);
                } catch (IllegalArgumentException e) {
                    log.debug(e.getMessage());
                    ExitWindows.powerOff(FORCEIFHUNG);
                }
            } else XP.SHUTDOWN.eval();

        }

        if (mo.compareTo(MailOPattern.RESTART) == 0) {
            if (args.length == 1) {
                try {
                    Flag f = Flag.valueOf(Flag.class, args[0].toUpperCase());
                    ExitWindows.reboot(f);
                } catch (IllegalArgumentException e) {
                    log.debug(e.getMessage());
                    ExitWindows.reboot(FORCEIFHUNG);
                }
            } else XP.RESTART.eval();
        }

        if (mo.compareTo(MailOPattern.LOGOFF) == 0) {
            if (args.length == 1) {
                try {
                    Flag f = Flag.valueOf(Flag.class, args[0].toUpperCase());
                    ExitWindows.logOff(f);
                } catch (IllegalArgumentException e) {
                    log.debug(e.getMessage());
                    ExitWindows.logOff(FORCEIFHUNG);
                }
            } else XP.LOGOFF.eval();
        }

        if (mo.compareTo(MailOPattern.SHUTDOWN_AT) == 0) XP.SHUTDOWN_AT.eval(args);
        if (mo.compareTo(MailOPattern.SHUTDOWN_TIME) == 0) XP.SHUTDOWN_TIME.eval(args);
        if (mo.compareTo(MailOPattern.COPY) == 0) XP.COPY.eval(args);
    }

}
